<template>
  <!-- 这是收货地址组件 -->
  <view>
    <!-- 收货地址的选择box -->
    <view class="address-choose-box" v-if="JSON.stringify(address)=='{}'">
      <button type="primary" size="mini" class="address-choose-button" @click="chooseAddress">请选择收货地址+</button>
    </view>
    
    <!-- 收货地址信息box -->
    <view class="address-info-box" v-else @click="chooseAddress">
      <!-- 第一行 -->
      <view class="row1">
        <view class="row1-left">收货人：{{address.userName}}</view>
        <view class="row1-right">
          <view class="iphone">电话：{{address.telNumber}}</view>
          <uni-icons type="right" size="16"></uni-icons>
        </view>
      </view>
      <!-- 第二行 -->
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{addstr}}</view>
      </view>
    </view>
    <!-- 底部的分割线图标 -->
    <image src="/static/my-icons/cart_border@2x.png" class="address-border"></image>
  </view>
</template>

<script>
  import { mapMutations, mapState, mapGetters} from 'vuex'
  
  export default {
    name:"my-address",
    computed:{
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['addstr'])
    },
    data() {
      return {
        
      };
    },
    methods:{
      ...mapMutations('m_user', ['updateAddress']),
      async chooseAddress(){
        const [err, succ] = await uni.chooseAddress({}).catch(err => err)
        console.log(err, succ)
        if(err == null && succ.errMsg == "chooseAddress:ok"){
          this.updateAddress(succ)
        }else uni.$showMsg('获取失败！')    
        
        // 如果用户没有授权 重新拉起授权
        // if(err && err.errMsg ===   'chooseAddress: fail auth deny'){
        //   // 调用方法 重新询问授权
        //   this.reAuth()
        // }
      }
    }
  }
</script>

<style lang="scss">
.address-border{
  display: block;
  width: 100%;
  height: 5px;
}
.address-choose-box{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 90px;
}
.address-info-box{
  justify-content: center;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  height: 90px;
  width: 100%;
  padding: 5px 5px;
  .row1{
    display: flex;
    justify-content: space-between;
    .row1-left{
      
    }
    .row1-right{
      display: flex;
      justify-content: space-between;
    }
  }
  .row2{
    display: flex;
    margin-top: 10px;
    align-items: center;
    .row2-left{
      display: flex;
      align-items: center;
      white-space: nowrap;
    }
    .row2-right{
      margin-right: 5px;
    }
  }
}

</style>
